package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.vo.chapter.ChapterVo;
import com.atguigu.eduservice.feign.VodServiceApi;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-08
 */
@Api(description = "章节服务")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private VodServiceApi vodServiceApi;

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 根据课程id查询
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideo(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        chapterService.save(eduChapter);

        return R.ok();
    }

    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter=chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
//        List<EduVideo> list = eduVideoService.list(new QueryWrapper<EduVideo>().lambda().eq(EduVideo::getChapterId, chapterId));
//        for (EduVideo eduVideo : list) {
//            String id = eduVideo.getVideoSourceId();
//            if (!StringUtils.isEmpty(id)) {
//                vodServiceApi.removeAliyunVideo(id);
//            }
//        }
        int flag = chapterService.deleteChapter(chapterId);
        if (flag > 0) {
            return R.ok();
        } else {
            throw new GuliException(20001, "删除章节失败");
        }

    }
}

